<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    
  </style>
</head>
<body>
  <h1>这是注册页面</h1>
  <input type="text" class="un"><br>
  <input type="text" class="pw"><br>
  <input type="button" class="btn" value="注册">
  <a href="./1登录.html">去登录</a>
</body>
<script src="./ajax.js"></script>
<script src="../toast.js"></script>
<script>

  $(".btn").onclick = function(){
    ajax({
      url: "http://10.11.50.46:3000/register",
      data: {
        username: $(".un").value,
        password: $(".pw").value
      },
      method:"post",
      success:res=>{
        res = JSON.parse(res);
        if(res.code === 0){
          Toast.init({
            type: "error",
            title: res.msg,
            close: ()=>{
              $(".un").value = "";
              $(".un").focus();
            }
          })
        }else{
          Toast.init({
            type: "success",
            title: res.msg,
            close:function(){
              const obj = {
                username: $(".un").value,
                password: $(".pw").value
              }
              sessionStorage.setItem("userMsg", JSON.stringify(obj));
              location.assign("./1登录.html");
            }
          })
        }
      }
    })
  }
  

  function $(s){
    return document.querySelector(s)
  }

</script>
</html>